package com.blogs.service;

import java.net.URLEncoder;
import java.util.Map;

import com.blogs.entity.AppConfig;
import com.blogs.entity.MsgEntity;
import com.blogs.entity.AppEnums.EnumContentType;
import com.blogs.entity.SendCommentEntry;
import com.blogs.service.GetMsgTask.onTaskCallBack;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class SendComment {
	public interface IMsgCallBack {
		public void onSuccess(SendCommentEntry sce);

		public void onFailed();
	}

	private String url = "";
	private GetMsgTask getresult;
	private IMsgCallBack callback;

	public SendComment(int is_add_from, int type,String sid, String comment_id,String commendBody,String parentCommentID,String blogapp, IMsgCallBack callback) {
		// TODO Auto-generated constructor stub
		this.callback = callback;
		commendBody = URLEncoder.encode(commendBody);
		if(type==EnumContentType.blog||type==EnumContentType.search_blog_detail)
		{
			url=AppConfig.SEND_BLOG_COMMENT.replace("{0}", sid).replace("{1}", comment_id).replace("{2}", commendBody).replace("{3}", parentCommentID).replace("{4}", is_add_from+"").replace("{5}", blogapp+"");
		}
		else
		{
			url=AppConfig.SEND_NEWS_COMMENT.replace("{0}", sid).replace("{1}", comment_id).replace("{2}", commendBody).replace("{3}", parentCommentID).replace("{4}", is_add_from+"");
		}
		getresult = new GetMsgTask(taskCallBack);
	}
	public void Send()
	{
		getresult.execute(url);
	}
	private GetMsgTask.onTaskCallBack taskCallBack = new onTaskCallBack() {

		public void onSuccess(String result) {
			// TODO Auto-generated method stub
			GsonBuilder builder = new GsonBuilder();
			Gson gson = builder.create();
			MsgEntity<Map<String, Object>> msg = null;
			try {
				msg = gson.fromJson(result, MsgEntity.class);
			} catch (Exception e) {
				callback.onFailed();
				return;
			}
			SendCommentEntry sce=new SendCommentEntry(msg.data);
			callback.onSuccess(sce);
		}

		public void onFailed() {
			// TODO Auto-generated method stub
			callback.onFailed();
		}
	};
}
